﻿namespace Huayou.Common
{
    using Huayou.Common.Database;
    using Huayou.Utils;
    using System;
    using System.Collections;
    using System.Data;
    using System.Web.UI.WebControls;

    public class CommonBaseBusiness : IDisposable
    {
        protected DBOperate curDBOperater;
        private int m_code;
        private string m_dbFieldList;
        private string m_dbTable;
        private string m_filter;
        private string m_group;
        private string m_msg;
        private string m_order;
        private bool selfOp;
        private Stack stack;

        public CommonBaseBusiness(DBOperate DBOper)
        {
            this.curDBOperater = null;
            this.selfOp = false;
            this.m_filter = null;
            this.m_group = null;
            this.m_order = null;
            this.m_dbFieldList = null;
            this.m_dbTable = null;
            this.stack = null;
            this.m_code = 0;
            this.m_msg = null;
            this.curDBOperater = DBOper;
            if (this.curDBOperater.baseConnection.State == ConnectionState.Closed)
            {
                this.curDBOperater.Open();
            }
            this.selfOp = false;
        }

        public CommonBaseBusiness(string connectString)
        {
            this.curDBOperater = null;
            this.selfOp = false;
            this.m_filter = null;
            this.m_group = null;
            this.m_order = null;
            this.m_dbFieldList = null;
            this.m_dbTable = null;
            this.stack = null;
            this.m_code = 0;
            this.m_msg = null;
            this.curDBOperater = DBOperatorFactory.GetDBOperator(connectString);
            this.curDBOperater.Open();
            this.selfOp = true;
        }

        public CommonBaseBusiness(string strDBServer, string strDBName, string strDBUser, string strDBPwd, int iDBType)
        {
            this.curDBOperater = null;
            this.selfOp = false;
            this.m_filter = null;
            this.m_group = null;
            this.m_order = null;
            this.m_dbFieldList = null;
            this.m_dbTable = null;
            this.stack = null;
            this.m_code = 0;
            this.m_msg = null;
            this.curDBOperater = DBOperatorFactory.GetDBOperator(strDBServer, strDBName, strDBUser, strDBPwd, iDBType);
            this.curDBOperater.Open();
            this.selfOp = true;
        }

        public void BeginTran()
        {
            this.curDBOperater.BeginTran();
        }

        public void CleanStatus()
        {
            this.DBFieldList = null;
            this.DBTable = null;
            this.Filter = null;
            this.Order = null;
            this.Group = null;
        }

        public void Close()
        {
            this.curDBOperater.Close();
        }

        public void CommitTran()
        {
            this.curDBOperater.CommitTran();
        }

        protected DataTable CommonGetList()
        {
            string strSql = "select " + this.DBFieldList + " from " + this.DBTable;
            if (this.Filter != null)
            {
                strSql = strSql + " where " + this.Filter;
            }
            if (this.Group != null)
            {
                strSql = strSql + " group by " + this.Group;
            }
            if (this.Order != null)
            {
                strSql = strSql + " order by " + this.Order;
            }
            return this.curDBOperater.ExecQueryForDataTable(strSql);
        }

        public void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected void Dispose(bool disposing)
        {
            if (disposing && (this.selfOp && (this.curDBOperater != null)))
            {
                this.curDBOperater.Dispose();
                this.curDBOperater = null;
            }
        }

        protected DataTable GetListWithSP(string spName, IDataParameter[] para)
        {
            return this.curDBOperater.ExecProcForDataTable(spName, para);
        }

        public void PopStatus()
        {
            if (this.stack != null)
            {
                object obj2 = null;
                obj2 = this.stack.Pop();
                if (obj2 != null)
                {
                    this.m_group = obj2.ToString();
                }
                else
                {
                    this.m_group = null;
                }
                obj2 = this.stack.Pop();
                if (obj2 != null)
                {
                    this.m_order = obj2.ToString();
                }
                else
                {
                    this.m_order = null;
                }
                obj2 = this.stack.Pop();
                if (obj2 != null)
                {
                    this.m_filter = obj2.ToString();
                }
                else
                {
                    this.m_filter = null;
                }
                obj2 = this.stack.Pop();
                if (obj2 != null)
                {
                    this.m_dbTable = obj2.ToString();
                }
                else
                {
                    this.m_dbTable = null;
                }
                obj2 = this.stack.Pop();
                if (obj2 != null)
                {
                    this.m_dbFieldList = obj2.ToString();
                }
                else
                {
                    this.m_dbFieldList = null;
                }
            }
        }

        public void PushStatus()
        {
            if (this.stack == null)
            {
                this.stack = new Stack();
            }
            this.stack.Push(this.DBFieldList);
            this.stack.Push(this.DBTable);
            this.stack.Push(this.Filter);
            this.stack.Push(this.Order);
            this.stack.Push(this.Group);
        }

        public void RollBackTran()
        {
            this.curDBOperater.RollBackTran();
        }

        public static void SetPage(DataGrid dg, IList list)
        {
            DataGridUtils.SetPage(dg, list);
        }

        public static void SetPage(DataGrid dg, DataTable dt)
        {
            DataGridUtils.SetPage(dg, dt);
        }

        public int Code
        {
            get
            {
                return this.m_code;
            }
            set
            {
                this.m_code = value;
            }
        }

        public string DBFieldList
        {
            get
            {
                return this.m_dbFieldList;
            }
            set
            {
                this.m_dbFieldList = value;
            }
        }

        public string DBTable
        {
            get
            {
                return this.m_dbTable;
            }
            set
            {
                this.m_dbTable = value;
            }
        }

        public string Filter
        {
            get
            {
                if ((this.m_filter == null) || this.m_filter.Equals(""))
                {
                    return null;
                }
                return this.m_filter;
            }
            set
            {
                if (value == null)
                {
                    this.m_filter = null;
                }
                else if (!value.Equals(""))
                {
                    if ((this.m_filter == null) || this.m_filter.Equals(""))
                    {
                        this.m_filter = value;
                    }
                    else
                    {
                        this.m_filter = this.m_filter + " and " + value;
                    }
                }
            }
        }

        public string Group
        {
            get
            {
                if ((this.m_group == null) || this.m_group.Equals(""))
                {
                    return null;
                }
                return this.m_group;
            }
            set
            {
                this.m_group = value;
            }
        }

        public string Msg
        {
            get
            {
                return this.m_msg;
            }
            set
            {
                this.m_msg = value;
            }
        }

        public string Order
        {
            get
            {
                if ((this.m_order == null) || this.m_order.Equals(""))
                {
                    return null;
                }
                return this.m_order;
            }
            set
            {
                this.m_order = value;
            }
        }

        public string OrFilter
        {
            set
            {
                if (value == null)
                {
                    this.m_filter = null;
                }
                else if (!value.Equals(""))
                {
                    if ((this.m_filter == null) || this.m_filter.Equals(""))
                    {
                        this.m_filter = value;
                    }
                    else
                    {
                        this.m_filter = this.m_filter + " or " + value;
                    }
                }
            }
        }

        public string RawFilter
        {
            set
            {
                if (value == null)
                {
                    this.m_filter = null;
                }
                else if (!value.Equals(""))
                {
                    if ((this.m_filter == null) || this.m_filter.Equals(""))
                    {
                        this.m_filter = value;
                    }
                    else
                    {
                        this.m_filter = this.m_filter + " " + value;
                    }
                }
            }
        }
    }
}

